package com.ifinc.common.protocol.i8583.field;

import com.ifinc.common.utils.StringUtils;
import com.ifinc.common.utils.encode.BcdUtils;

/**
 * User: luotao
 * Date: 13-11-15
 * Time: 13:41
 */
public class FieldBcd extends AbstractField {

    public FieldBcd() {
    }

    public FieldBcd(int fieldLen) {
        super(fieldLen);
    }

    public FieldBcd(int fieldLen, boolean joinBitMap) {
        super(fieldLen, joinBitMap);
    }

    @Override
    public byte[] toByte() {
        String str = StringUtils.leftPadWithBytes(getSrcValue(),getFieldLen(),'0',getEncoding());
        resBytes = BcdUtils.str2Bcd(str);
        return setResDataInfo(resBytes,str);
    }

    public byte[] setResDataInfo(byte[] bytes,String resValue) {
        setResBytes(bytes);
        setResValue(resValue);
        return bytes;
    }

    @Override
    public byte[] toMacByte() {
        return str2byte(resValue);
    }

    @Override
    public String getFieldType() {
        return BCD;
    }

    @Override
    public void verify() {
    }

    @Override
    public int toValue(byte[] data, int index) {
        return 0;
    }

    @Override
    public int getEncodeLen() {
        return getFieldLen()/100+1;
    }
}
